﻿define(["require", "exports"], function(require, exports) {
    (function (DateUtils) {
        function getFromMSString(date) {
            var sDate = date.replace("\/Date(", "").replace(")\/", "");
            return new Date(parseInt(sDate));
        }
        DateUtils.getFromMSString = getFromMSString;
        ;
        function getDatePart(date, format) {
            if(typeof date == "string") {
                date = getFromMSString(date);
            }
            switch(format) {
                case "yyyy":
                    return date.getFullYear().toString();
                case "yy":
                    return date.getFullYear().toString().substr(2, 2);
                case "MM":
                    var month = date.getMonth() + 1;
                    return (month >= 10 ? "" : "0") + month.toString();
                case "M":
                    return (date.getMonth() + 1).toString();
                case "dd":
                    return (date.getDate() >= 10 ? "" : "0") + date.getDate().toString();
                case "d":
                    return date.getDate().toString();
                case "HH":
                    var hour = date.getHours();
                    return (hour >= 10 ? "" : "0") + hour.toString();
                case "H":
                    return date.getHours().toString();
                case "mm":
                    var minute = date.getMinutes();
                    return (minute >= 10 ? "" : "0") + minute.toString();
                case "m":
                    return date.getMinutes().toString();
                case "ss":
                    var sec = date.getSeconds();
                    return (sec >= 10 ? "" : "0") + sec.toString();
                case "s":
                    return date.getSeconds().toString();
                default:
                    return format;
            }
        }
        ;
        function formatDate(date, format) {
            if(format == null || date == null) {
                return "";
            }
            if(typeof date == "string") {
                date = getFromMSString(date);
            }
            var originalString = format;
            var currentlyParsed = null;
            var lastChar = null;
            var isInText = false;
            var output = "";
            var separatorChars = [
                " ", 
                "-", 
                ".", 
                ":", 
                "_"
            ];
            var isSeparatorChar = function (input) {
                for(var i = 0; i < separatorChars.length; i++) {
                    if(input == separatorChars[i]) {
                        return true;
                    }
                }
                return false;
            };
            for(var i = 0; i < format.length; i++) {
                var curChar = format.charAt(i);
                var isSepChar = isSeparatorChar(curChar);
                if(isInText) {
                    if(curChar == "\"") {
                        isInText = false;
                        currentlyParsed = null;
                    } else {
                        output += curChar;
                    }
                } else if(curChar == "\"") {
                    isInText = true;
                } else if(currentlyParsed == null) {
                    currentlyParsed = curChar;
                } else if(isSepChar || curChar != lastChar || i == format.length - 1) {
                    if(!isSepChar) {
                        currentlyParsed += curChar;
                    }
                    output += getDatePart(date, currentlyParsed);
                    currentlyParsed = null;
                    if(isSepChar) {
                        output += curChar;
                    }
                } else {
                    currentlyParsed += curChar;
                }
                lastChar = curChar;
            }
            return output;
        }
        DateUtils.formatDate = formatDate;
        ;
        function formatGerman(input) {
            if(typeof input == "string") {
                input = getFromMSString(input);
            }
            return formatDate(input, "dd.MM.yyyy");
        }
        DateUtils.formatGerman = formatGerman;
        ;
        function formatISO(input) {
            if(typeof input == "string") {
                input = getFromMSString(input);
            }
            return formatDate(input, "yyyy-MM-dd");
        }
        DateUtils.formatISO = formatISO;
        ;
        function parseISO(input) {
            if(input == null || input == "") {
                return null;
            }
            var parts = input.split("-");
            var year = parseInt(parts[0]);
            var month = parseInt(parts[1]);
            var day = parseInt(parts[2]);
            return new Date(year, month - 1, day);
        }
        DateUtils.parseISO = parseISO;
        ;
        function parseGerman(input) {
            if(input == null || input == "") {
                return null;
            }
            var parts = input.split(".");
            var year = parseInt(parts[2]);
            var month = parseInt(parts[1]);
            var day = parseInt(parts[0]);
            return new Date(year, month - 1, day);
        }
        DateUtils.parseGerman = parseGerman;
        ;
        function parseDate(input) {
            if(input == null || input == "") {
                return null;
            }
            if(input.indexOf(".") !== -1) {
                return parseGerman(input);
            } else {
                return parseISO(input);
            }
        }
        DateUtils.parseDate = parseDate;
    })(exports.DateUtils || (exports.DateUtils = {}));
    var DateUtils = exports.DateUtils;
})
//@ sourceMappingURL=DateUtils.js.map
